package com.leetcode.offer.chapter5;

import java.util.Arrays;
import java.util.PriorityQueue;

/**
 * @author Dennis Li
 * @date 2020/7/17 21:13
 */
public class MinNumber_45 {

    public String minNumber(int[] nums) {
        PriorityQueue<Integer> queue = new PriorityQueue<>((o1, o2) -> ("" + o1 + o2).compareTo(("" + o2 + o1)));
        for (int num : nums) queue.offer(num);
        StringBuilder sb = new StringBuilder();
        while (!queue.isEmpty()) sb.append(queue.poll());
        return sb.toString();
    }

    public String minNumbersBetter(int[] nums) {
        String[] strings = new String[nums.length];
        for (int i = 0; i < nums.length; i++)
            strings[i] = String.valueOf(nums[i]);
        // 注意比较器里，compareTo方法的使用！！！！！！！
        Arrays.sort(strings, (o1, o2) -> (o1 + o2).compareTo(o2 + o1));
        StringBuilder sb = new StringBuilder();
        for (String string : strings) sb.append(string);
        return sb.toString();
    }

}
